package top.jafar.data.structures;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class QueueDemo {
    public static void main(String[] args) {
        boolean loop = true;
//        ArrayQueue<Integer> queue = new ArrayQueue<>(4);
        LinkedQueue queue = new LinkedQueue(3);
        char key;
        while (loop) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的操作:\n" +
                    "a: (append)\th: (get the head one)\n" +
                    "g: (get)\tt: (get the last one)\n" +
                    "s: (show)\tq: (quite)");
            key = scanner.next().charAt(0);
            switch (key) {
                case 'a':
                    System.out.print("请输入您要加入的元素: ");
                    int num = scanner.nextInt();
                    queue.offer(num);
                    break;
                case 'h':
                    System.out.println("队列的头部元素为: "+queue.getHeader());
                    break;
                case 'g':
                    System.out.println("获取到的元素为: " + queue.take());
                    break;
                case 't':
                    System.out.println("队列的尾部元素为: " + queue.getTail());
                    break;
                case 's':
                    queue.showList();
                    break;
                case 'q':
                    System.exit(0);
                    break;
                default:
                    System.out.println("不支持的操作类型: "+key);
            }
        }
    }
}

// 链表队列
class LinkedQueue {
    // 头部指针
    private int front;
    // 尾部指针
    private int rear;
    // 最大队列数量
    private int maxSize;
    // 队列数组
    private int[] list;

    public LinkedQueue(int maxSize) {
        this.maxSize = maxSize;
        front = 0;
        rear = 0;
        list = new int[maxSize];
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return front != rear && (front)%maxSize == rear;
    }

    public void offer(int num) {
        if (isFull()) {
            System.err.println("队列已满");
        }else {
            list[front%maxSize] = num;
            front++;
        }
    }

    public Integer take() {
        if (isEmpty()) {
            System.err.println("队列中暂无数据");
            return null;
        }
        rear++;
        return list[(rear-1)%maxSize];
    }

    public Integer getHeader() {
        if (isEmpty()) {
            return null;
        }else {
            return list[(front - 1) % maxSize];
        }
    }

    public Integer getTail() {
        if (isEmpty()) {
            return null;
        }
        return list[rear%maxSize];
    }

    public void showList() {
        System.out.print("队列中的元素: [");
        if (!isEmpty()) {
            for (int i = rear; i < front; i++) {
                System.out.print(list[i%maxSize]+", ");
            }
        }
        System.out.println("]");
    }

}

// 数组队列
class ArrayQueue<T> {
    private int maxSize; //最大容量
    private int front; // 头部指针
    private T[] list; //容器

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.list = (T[]) new Object[maxSize];
    }

    public T getHeader() {
        return list[0];
    }

    public T getTail() {
        return list[front-1];
    }

    public void showList() {
        System.out.print("当前队列中的元素: [");
        for (int i = 0; i < front; i++) {
            System.out.print(list[i]+", ");
        }
        System.out.println("]");
    }

    public void offer(T obj) {
        if (front < maxSize) {
            list[front] = obj;
            front++;
        }else {
            System.err.println("队列已满!");
        }
    }

    public T take() {
        if (front == 0) {
            return null;
        }
        T obj = list[0];
        // 将数组中的值向下顺移
        for (int i = 0; i < list.length; i++) {
            if (i < list.length - 1) {
                list[i] = list[i+1];
            }
        }
        front--;
        return obj;
    }


}